package mapper;

import dto.AdminLoginDTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import utilities.ConnectDB;


public class AdminLoginMapper {
    
    public boolean login(String username,String pass)
    {
        boolean flag = false;
        try {
            String qry = "select * from AdminLogin where username = '"+username+"' and pass = '"+pass+"'";
            ResultSet rs = ConnectDB.getResultSet(qry);
            if(rs != null && rs.next())
            {
                flag = true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(AdminLoginMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
        return flag;

    }
    public boolean changePassword(String username,String pass)
    {
        String qry = "update AdminLogin set pass = '"+pass+"' where username = '"+username+"'";
        int flag = ConnectDB.executeQueryString(qry);
        if(flag == 1)
        {
            return true;
        }
        return false;

    }
    public boolean addAdminAccount(AdminLoginDTO ad)
    {

        String qry = "insert into AdminLogin(username,pass) values(N'"+ad.getUsername()+"',N'"+ad.getPass()+"')";
        int flag = ConnectDB.executeQueryString(qry);
        if(flag == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public AdminLoginDTO getAdminAccount(String username)
    {
        AdminLoginDTO adDTO = new AdminLoginDTO();
        try {
            String qry = "select * from AdminLogin where username = '"+username+"'";
            ResultSet rs = ConnectDB.getResultSet(qry);
            if(rs != null && rs.next())
            {
                initAdmin(adDTO, rs);

            }
        } catch (SQLException ex) {
            Logger.getLogger(AdminLoginMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
        return adDTO;
    }
    public void initAdmin(AdminLoginDTO ad , ResultSet rs)
    {
        try {
            ad.setUsername(rs.getString(2));
            ad.setPass(rs.getString(3));
            
        } catch (SQLException ex) {
            Logger.getLogger(AdminLoginMapper.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    public boolean updateAdminAccount(AdminLoginDTO ad)
    {

        String qry = "Update AdminLogin Set pass = '"+ad.getPass()+"' where username = '"+ad.getUsername()+"'";
        int flag = ConnectDB.executeQueryString(qry);
        if(flag == 1)
        {
            return true;

        }
        else
        {
            return false;
        }
    }
    
}
